﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

//微软c#基础教程地址
//https://learn.microsoft.com/zh-cn/dotnet/csharp/fundamentals/tutorials/classes

//视频教程地址：
//https://www.bilibili.com/video/BV1hV4y1L785?p=16&spm_id_from=pageDriver&vd_source=aade28408008fe0ffee5016cb40b8051
namespace Class06_类和对象
{
    //添加一个类，命名为BankAccount.cs
    /// <summary>
    /// 创建银行账户的方法
    /// </summary>
    //快捷键：summary的快捷键，在方法前一行键入///自动弹出summary注释
    public class BankAccount
    {
        //新增几个属性，get表示属性可读，set表示属性可写
        public string Number { get; }
        public string Owner { get; set; }
        //键入prop+Tab 新增属性的快捷方式

        // public decimal Balance { get;  }
        public decimal Balance {
            get
            { //修改一下，将Balance变成一种行为
                decimal balance = 0;
                foreach (var item in allTransactions)//根据操作历史记录计算余额
                {
                    //  balance = balance + item.Amount;
                    balance += item.Amount;//简化写法
                }
                return balance;
            }
            set { }
        }

        private static int accountNumberSeed = 1234567890;// private表示私有的，只能在BankAccount类中看到

        private List<Transaction> allTransactions = new List<Transaction>();//创建一个自定义数据类型

        public BankAccount(string name,decimal initialBanalance)//构造函数
        {
            // this.Owner = name;//仅当局部变量或参数具有与该字段或属性相同的名称时，才需要限定符 this。 除非有必要，否则在本文的其余部分中省略限定符 this。
            Owner = name;
            Balance = initialBanalance;
            MakeDeposit(initialBanalance, DateTime.Now, "Initial balance");
            this.Number = accountNumberSeed.ToString();
            accountNumberSeed++;
        }

        //下节课继续：
        //https://www.bilibili.com/video/BV1hV4y1L785?p=18&spm_id_from=pageDriver&vd_source=aade28408008fe0ffee5016cb40b8051
        public void MakeDeposit(decimal amount, DateTime date, string note)
        {
            if (amount <= 0)//存款额不能小于0
            {
                //抛出异常
                throw new ArgumentOutOfRangeException(nameof(amount), "Amount of deposit must be positive");
            }
            var deposit = new Transaction(amount, date, note);//存款
            allTransactions.Add(deposit);//添加存款记录
        }

        public void MakeWithdrawal(decimal amount, DateTime date, string note)
        {
            //取款额不能小于0，且计算去看后余额不能小于0
            if (amount <= 0)
            {
                throw new ArgumentOutOfRangeException(nameof(amount), "Amount of withdrawal must be positive");
            }
            if (Balance - amount < 0)
            {
                throw new InvalidOperationException("Not sufficient funds for this withdrawal");
            }
            var withdrawal = new Transaction(-amount, date, note);//取款
            allTransactions.Add(withdrawal);//添加取款记录
        }
        /// <summary>
        /// 账户操作历史记录
        /// </summary>
        /// <returns></returns>
        public string GetAccountHistory()
        {
            var report = new StringBuilder();
            //表头
            report.AppendLine("Date\t\t\t Ammount\t Note");
            foreach (var item in allTransactions)
            {
                //表的一行
                report.AppendLine($"{item.Date}\t{item.Amount}\t{item.Notes}");
            }
            return report.ToString();
        }

    }
}
